<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm, handleQuery }"> </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['three:meanwhile:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      :rowClassName="rowClassName"
    >
      <template #default>
        <!-- <el-table-column
          label="安全许可证附件"
          align="center"
          key="updateTime"
          prop="updateTime"
          width="200"
        >
          <template #default="{ row }">
            <el-button
              type="text"
              icon="Edit"
              :disabled="!row.permitFlieUrl"
              @click="handleDownload(row.permitFlieUrl)"
              >下载</el-button
            >
          </template>
        </el-table-column> -->
        <el-table-column
          label="报告附件"
          align="center"
          key="updateTime"
          prop="updateTime"
          width="200"
        >
          <template #default="{ row }">
            <el-button
              type="text"
              icon="Edit"
              :disabled="!row.fileUrl"
              @click="handleDownload(row)"
              >下载</el-button
            >
          </template>
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['three:meanwhile:edit']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['three:meanwhile:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSlot']"
      :title="dialogType == 'add' ? '新增三同时手续' : '修改三同时手续'"
    >
      <template #fileSlot>
        <!-- <el-form-item label="许可证附件">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="permitFile"
          ></FileUpload>
        </el-form-item> -->
        <el-form-item label="报告附件">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="file"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import FileUpload from "@/components/FileUpload";
import { apis as APIS } from "@/api/aqsc/stsgl.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { useRoute } from "vue-router";
const route = useRoute();
const path = route.fullPath.split("/").pop();
const type = path == "aqxkz" ? 2 : 1;
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["project", "name", "major", "category", "validEnd", "validStart"].includes(
      i.prop
    )
  ),
});
const permitFile = ref([]);
const file = ref([]);

watch(
  () => file,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);

const handleDownload = (row) => {
  window.downLoadFile(row.fileUrl, row.fileName);
};
const showSearch = ref(true);

const deptName = ref("");
const plugAdd = () => {
  editRow.value = {};
  file.value = [];
  form.value.type = 2;
};
const editRow = ref({});
const pludEdit = (row) => {
  editRow.value = { ...row };
  if (row.permitFileId) {
    file.value = [
      {
        name: row.permitFileName,
        id: row.permitFileId,
        url: row.permitFlieUrl,
      },
    ];
  } else {
    file.value = [];
  }
  if (row.fileId) {
    permitFile.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  } else {
    permitFile.value = [];
  }
};
const table = ref();
const dialogVform = ref();
const {
  onSubmit,
  fetchData,
  dialogType,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugAdd,
  pludEdit,
  filterForm: {
    type: 2,
  },
});

const rowClassName = ({ row, rowIndex }) => {
  const nowTime = Date.now();
  const endTime = new Date(row.validEnd).getTime();
  const txTime = new Date(row.remindDate).getTime();
  if (endTime && nowTime >= endTime) {
    return "deepred";
  } else if (txTime && nowTime >= txTime && nowTime <= endTime) {
    return "yellow";
  } else {
    return "";
  }
};
</script>

<style lang="scss" scoped></style>
